package sy.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.AppAccountVerificatCodeDaoI;
import sy.model.AppAccountVerificatCode;
@Repository("appAccountVerificatCodeDao")
public class AppAccountVerificatCodeDaoImpl implements AppAccountVerificatCodeDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	@Override
	public Serializable save(AppAccountVerificatCode appAccountVerificatCode) {
		// TODO Auto-generated method stub
		return getCurrentSession().save(appAccountVerificatCode);
	}

	@Override
	public boolean judgeTelephoneIsExist(String telephone) {
		// TODO Auto-generated method stub
		String hql = "from AppAccountVerificatCode where account = :telephone";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("telephone", telephone);
		List<AppAccountVerificatCode> list = query.list();
		if (!list.isEmpty()) {
			return true;
		}
		return false;
	}

	@Override
	public Serializable updateAppAccountVerificatCode(String telephone, String verificatCode,
			Date creatTime) {
		// TODO Auto-generated method stub
		String hql = "update AppAccountVerificatCode set verificatCode = ?, createTime = ? where account = ?";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameter(0, verificatCode);
		query.setParameter(1, creatTime);
		query.setParameter(2, telephone);
		return query.executeUpdate();
	}

	@Override
	public List<AppAccountVerificatCode> getAppAccountVerificatCode(
			String telephone, String verificatCode) {
		// TODO Auto-generated method stub
		String hql = "from AppAccountVerificatCode where account = :telephone and verificatCode = :verificatCode";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("telephone", telephone);
		query.setString("verificatCode", verificatCode);
		List<AppAccountVerificatCode> list = query.list();
		return list;
	}
	
}
